/*
	Copyright (c) 2008, Grant Jones <grant@podtronics.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

		Redistributions of source code must retain the above copyright
		notice, this list of conditions and the following disclaimer.
		
		Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in
		the documentation and/or other materials provided with the
		distribution.
		
		Neither the name of the gxjones.com nor the names of its
		contributors may be used to endorse or promote products derived
		from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
	OF THE POSSIBILITY OF SUCH DAMAGE.
	
	http://www.opensource.org/licenses/bsd-license.php
*/

/*
 *  strmap.c
 *  scatterd
 *
 *  Created by Grant Jones on 3/3/08.
 *
 */

#include "strmap.h"


unsigned long strmap_hash( const char *key, size_t key_sz )
{
#ifndef TEST
	return djb2_hash(key, key_sz);
#else	
	return 1;
#endif
}


str_entry_t *strmap_lookup_raw( cmapStruct *cmap, const char *key, size_t key_sz, str_entry_t **first_se )
{
	unsigned long hkey = strmap_hash(key, key_sz);
	str_entry_t *se = cmap_lookup(cmap, hkey);

	if( first_se )
		*first_se = se;

	/* linear search, meh: */
	while( se && (se->key_sz != key_sz || memcmp( key, se->key, key_sz ) != 0) )
		se = se->link;
	return se;
}

void *strmap_lookup( cmapStruct *cmap, const char *key, size_t key_sz )
{
	str_entry_t *se = strmap_lookup_raw(cmap, key, key_sz, NULL);
	
	if( !se )
		return NULL;
	return se->data;
}


str_entry_t *strmap_assign( cmapStruct *cmap, const char *key, size_t key_sz, void *data )
{
	unsigned long hkey = strmap_hash(key, key_sz);
	str_entry_t *first_se = NULL;
	str_entry_t *existing_se = strmap_lookup_raw( cmap, key, key_sz, &first_se );

	if( existing_se )
	{
		existing_se->data = data;
		return existing_se;
	}
	
	
	str_entry_t *se = cmap_assign_new(cmap, hkey, sizeof(str_entry_t) + key_sz );
	if(!se)
		return NULL;
		
	se->data = data;
	se->key_sz = key_sz;
	se->link = first_se;
	memcpy( se->key, key, key_sz );

	return se;
}

int strmap_remove( cmapStruct *cmap, const char *key, size_t key_sz )
{
	unsigned long hkey = strmap_hash(key, key_sz);
	str_entry_t *first_se = cmap_lookup(cmap, hkey);
	str_entry_t *se = first_se;
	str_entry_t *prev_se = NULL;
	if(!se)
		return -1;

	/* linear search, meh: */
	while( se && (se->key_sz != key_sz || memcmp( key, se->key, key_sz ) != 0) )
	{
		prev_se = se;
		se = se->link;
	}
	
	if( se == first_se && se->link == NULL )
	{
		/* just one entry remains */
		cmap_remove( cmap, hkey );
#ifdef TEST		
printf("just one entry remains\n");
#endif
	}
	else
	{
		if( !prev_se )
		{
			/* next entry needs to be promoted to list head */
			assert( se->link != NULL );
			cmap_assign(cmap, hkey, se->link );
#ifdef TEST
printf("next entry needs to be promoted to list head\n");			
#endif
		}
		else
		{
			/* entry is in the middle somewhere */
			assert( prev_se->link == se );
			prev_se->link = se->link;
#ifdef TEST			
printf("entry is in the middle somewhere\n");
#endif
		}
			
	}
	mempool_free(cmap->mempool, se);	
	return 0;
}


void strmap_enum( cmapStruct *cmap, strmapEnumStruct *e )
{
	assert(cmap);
	cmap_enum(cmap, &e->e);
	e->cur = NULL;
}

void *strmap_enum_next( strmapEnumStruct *e, char **key_ptr, size_t *key_sz_ptr )
{
	unsigned long key_hash;
	if( e->cur == NULL || e->cur->link == NULL )
	{
		e->cur = cmap_enum_next(&e->e, (CMAP_KEY_TYPE *)&key_hash);
		if(!e->cur)
			return NULL;
	}
	else
	{
		assert( e->cur->link != NULL );
		e->cur = e->cur->link;
	}

	*key_ptr = e->cur->key;
	*key_sz_ptr = e->cur->key_sz;
	return e->cur->data;
}



#ifdef TEST
int main( int argc, char *argv[] )
{
	cmapStruct *c = cmap_new(NULL);
	
	char *key[] = {
		"key 1",
		"key 2",
		"key 3" };
	size_t key_sz[] = { strlen(key[0]), strlen(key[1]), strlen(key[2]) };
	
	
	strmap_assign(c, key[0], key_sz[0], key[0] );
	strmap_assign(c, key[1], key_sz[1], key[1] );
	strmap_assign(c, key[2], key_sz[2], key[2] );	
	
	printf("key[2]: %s\n", strmap_lookup(c, key[2], key_sz[2] ) );
	printf("key[1]: %s\n", strmap_lookup(c, key[1], key_sz[1] ) );
	printf("key[0]: %s\n", strmap_lookup(c, key[0], key_sz[0] ) );
	
	
	strmap_remove(c, key[2], key_sz[2] );

	printf("key[2]: %s\n", strmap_lookup(c, key[2], key_sz[2] ) );
	printf("key[1]: %s\n", strmap_lookup(c, key[1], key_sz[1] ) );
	printf("key[0]: %s\n", strmap_lookup(c, key[0], key_sz[0] ) );


	strmap_remove(c, key[0], key_sz[0] );

	printf("key[2]: %s\n", strmap_lookup(c, key[2], key_sz[2] ) );
	printf("key[1]: %s\n", strmap_lookup(c, key[1], key_sz[1] ) );
	printf("key[0]: %s\n", strmap_lookup(c, key[0], key_sz[0] ) );

	strmap_remove(c, key[1], key_sz[1] );

	printf("key[2]: %s\n", strmap_lookup(c, key[2], key_sz[2] ) );
	printf("key[1]: %s\n", strmap_lookup(c, key[1], key_sz[1] ) );
	printf("key[0]: %s\n", strmap_lookup(c, key[0], key_sz[0] ) );




	strmap_assign(c, key[0], key_sz[0], key[0] );
	strmap_assign(c, key[1], key_sz[1], key[1] );
	strmap_assign(c, key[2], key_sz[2], key[2] );	
	/*** ****/

	strmap_remove(c, key[1], key_sz[1] );

	printf("key[2]: %s\n", strmap_lookup(c, key[2], key_sz[2] ) );
	printf("key[1]: %s\n", strmap_lookup(c, key[1], key_sz[1] ) );
	printf("key[0]: %s\n", strmap_lookup(c, key[0], key_sz[0] ) );

	strmap_remove(c, key[0], key_sz[0] );

	printf("key[2]: %s\n", strmap_lookup(c, key[2], key_sz[2] ) );
	printf("key[1]: %s\n", strmap_lookup(c, key[1], key_sz[1] ) );
	printf("key[0]: %s\n", strmap_lookup(c, key[0], key_sz[0] ) );
	
	strmap_remove(c, key[2], key_sz[2] );

	printf("key[2]: %s\n", strmap_lookup(c, key[2], key_sz[2] ) );
	printf("key[1]: %s\n", strmap_lookup(c, key[1], key_sz[1] ) );
	printf("key[0]: %s\n", strmap_lookup(c, key[0], key_sz[0] ) );	
	
	cmap_destroy(c);


	return 0;
}
#endif




